package cn.bingo.yunsj.controller;

import cn.bingo.yunsj.common.exception.EmException;
import cn.bingo.yunsj.common.exception.OwnException;
import cn.bingo.yunsj.common.result.Response;
import cn.bingo.yunsj.domain.RpModel;
import cn.bingo.yunsj.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@Api("模板信息控制器")
@RequestMapping(value = "/model")
@CrossOrigin
public class ModelController {

    @Autowired
    private ModelService modelService;

    /**
     * 获取数据库所有模板信息
     */
    @RequestMapping(value = "/ALL",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取数据库所有模板信息")
    public Response getAll(){
        return modelService.getAll();
    }

    /**
     * 模板得启动
     */
    @RequestMapping(value = "/state",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("模板得启动")
    public Response state(RpModel rpModel){
        if(StringUtils.isEmpty(rpModel.getModelId())
                ||StringUtils.isEmpty(rpModel.getModelState())
                ||StringUtils.isEmpty(rpModel.getModelType())){
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return modelService.state(rpModel);
    }

    /**
     * 模板禁用
     */
    @RequestMapping(value = "/disable",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("模板禁用")
    public Response disable(RpModel rpModel){
        if(StringUtils.isEmpty(rpModel.getModelId())
                ||StringUtils.isEmpty(rpModel.getModelState())){
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return modelService.disable(rpModel);
    }

    /**
     * 查询模板
     */
    @RequestMapping(value = "search",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询模板")
    public Response search(RpModel rpModel){
        if(StringUtils.isEmpty(rpModel.getModelType())){
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return modelService.search(rpModel);
    }

    // 只查询没有规则得模板
    @RequestMapping(value = "/no_rule",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("只查询没有规则得模板")
    public Response getNoRule(){
        return modelService.getNoRule();
    }


    /**
     * 删除模板
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("删除邮件模板")
    public Response delete(RpModel rpModel){
        if(StringUtils.isEmpty(rpModel.getModelId())){
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return modelService.delete(rpModel);
    }

    //上传
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ApiOperation("邮件模板上传")
    public Response upload(@RequestParam("type") String type,
                       @RequestParam("file") MultipartFile file) {
        // 限制为念类型只能docx
        String path = file.getOriginalFilename();
        int index = path.indexOf(".");
        String fileType = path.substring(index+1,path.length());
        if(!StringUtils.equals(fileType,"docx")){
            throw new OwnException(EmException.TYPE_FAIL);
        }
        return modelService.upload(type,file);
    }

}
